package com.huifer.wgx.service.impl;

import com.huifer.wgx.geoverify.PointVerify;
import com.huifer.wgx.model.DetectionPointEntity;
import com.huifer.wgx.model.FieldPointEntity;
import com.huifer.wgx.model.InteriorPointEntity;
import com.huifer.wgx.model.PersonnelEntity;
import com.huifer.wgx.model.VerifyEntity;
import com.huifer.wgx.repository.DetectionPointEntityRepository;
import com.huifer.wgx.repository.FieldPointRepository;
import com.huifer.wgx.repository.InteriorPointRepository;
import com.huifer.wgx.repository.PersonnelRepository;
import com.huifer.wgx.repository.VerifyRepository;
import com.huifer.wgx.service.DetectionPointService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import sun.awt.geom.AreaOp.CAGOp;

/**
 * <p>Title : DetectionPointServiceImpl </p>
 * <p>Description : DetectionPointServiceImpl</p>
 *
 * @author huifer
 * @date 2019-03-20
 */
@Service
public class DetectionPointServiceImpl implements DetectionPointService {

    @Autowired
    private VerifyRepository verifyRepository;
    @Autowired
    private DetectionPointEntityRepository detectionPointEntityRepository;
    @Autowired
    private InteriorPointRepository interiorPointRepository;

    /**
     *
     */
    @Override
    public List<DetectionPointEntity> createList(Long verifyId, Long workId, Long personnelId) {
        Specification<InteriorPointEntity> ps = (Specification<InteriorPointEntity>) (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder
                .equal(root.get("workId"), workId);
        List<InteriorPointEntity> all = interiorPointRepository.findAll(ps);
        List<DetectionPointEntity> ds = PointVerify.onlyPoint(all, verifyId, personnelId);

        return ds;
    }

    /**
     * 根据校验内容查询
     *
     * @param verifyName 校验名称
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return 校验结果列表
     */
    @Override
    public Page<DetectionPointEntity> findByVerifyName(String verifyName, Integer pageNum,
            Integer pageSize) {
        try {

            VerifyEntity verifyEntity = getVerifyEntity(verifyName);

            if (verifyEntity != null) {

                Specification<DetectionPointEntity> specD = (Specification<DetectionPointEntity>) (root, criteriaQuery, criteriaBuilder) -> {
                    Predicate p1 = criteriaBuilder
                            .equal(root.get("verifyId"), verifyEntity.getVerifyId());

                    return p1;
                };
                return getDetectionEntities(pageNum, pageSize, specD);
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();

            return null;
        }
    }

    /**
     * 获取校验结果
     *
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @param specD 过滤条件
     * @return 校验结果列表
     */
    private Page<DetectionPointEntity> getDetectionEntities(Integer pageNum, Integer pageSize,
            Specification<DetectionPointEntity> specD) {
        Sort sort = new Sort(Direction.DESC, "detectionId");
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, sort);
        return detectionPointEntityRepository.findAll(specD, pageable);
    }

    /**
     * 保存校验结果列表
     *
     * @param detectionEntities 校验结果列表
     * @return 校验结果列表
     */
    @Override
    public List<DetectionPointEntity> saveDetectionList(
            List<DetectionPointEntity> detectionEntities) {
        try {
            return detectionPointEntityRepository.saveAll(detectionEntities);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据校验人员id查询
     *
     * @param personnelId 校验人员id
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return 校验结果列表
     */
    @Override
    public Page<DetectionPointEntity> findByPersonnelId(Long personnelId, Integer pageNum,
            Integer pageSize) {

        Specification<DetectionPointEntity> specV = (Specification<DetectionPointEntity>) (root, criteriaQuery, criteriaBuilder) -> {
            Predicate p1 = criteriaBuilder
                    .equal(root.get("personnelId"), personnelId);

            return p1;
        };
        return getDetectionEntities(pageNum, pageSize, specV);
    }


    /**
     * 根据校验人员名称查询
     *
     * @param personnelName 发起校验的人员名称
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return 校验结果列表
     */
    @Override
    public Page<DetectionPointEntity> findByPersonnelName(String personnelName, Integer pageNum,
            Integer pageSize) {
        Specification<PersonnelEntity> specP = (Specification<PersonnelEntity>) (root, criteriaQuery, criteriaBuilder) -> {
            Predicate p1 = criteriaBuilder
                    .equal(root.get("personnelName"), personnelName);
            return p1;
        };

        Page<DetectionPointEntity> all = getDetectionEntities(pageNum, pageSize);
        return all;
    }

    /**
     * 获取全部校验结果
     *
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return 校验结果列表
     */
    private Page<DetectionPointEntity> getDetectionEntities(Integer pageNum, Integer pageSize) {
        Sort sort = new Sort(Direction.DESC, "detectionId");
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, sort);
        return detectionPointEntityRepository.findAll(pageable);
    }


    /**
     * 根据 verifyName 查询
     *
     * @param verifyName 校验名称
     * @return 校验
     */
    private VerifyEntity getVerifyEntity(String verifyName) {
        Specification<VerifyEntity> specV = (Specification<VerifyEntity>) (root, criteriaQuery, criteriaBuilder) -> {
            Predicate p1 = criteriaBuilder
                    .equal(root.get("verifyName"), verifyName);

            return p1;
        };
        return verifyRepository.findOne(specV).get();
    }




    /**
     * 根据校验id查询
     *
     * @param verifyId 校验id
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return 校验结果列表
     */
    @Override
    public Page<DetectionPointEntity> findByVerifyId(Long verifyId, Integer pageNum,
            Integer pageSize) {
        Specification<DetectionPointEntity> specV = (Specification<DetectionPointEntity>) (root, criteriaQuery, criteriaBuilder) -> {
            Predicate p1 = criteriaBuilder
                    .equal(root.get("verifyId"), verifyId);

            return p1;
        };
        Page<DetectionPointEntity> all = getDetectionEntities(pageNum, pageSize, specV);
        return all;
    }

    /**
     * 查询所有
     *
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return 校验结果列表
     */
    @Override
    public Page<DetectionPointEntity> findAll(Integer pageNum, Integer pageSize) {
        Page<DetectionPointEntity> all = getDetectionEntities(pageNum, pageSize);
        return all;
    }
}
